package org.jeecg.controller;

import com.alibaba.fastjson.JSONObject;
import org.jeecg.domain.User;
import org.jeecg.result.ResultTool;
import org.jeecg.service.CareService;
import org.jeecg.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/care")
public class CareController {

    @Autowired
    private UserService userService;

    @Autowired
    private CareService careService;

    @RequestMapping(value = "/getCares",method = RequestMethod.GET)
    public ResultTool getCares(@RequestParam("username") String username){
        if (username == null){
            return new ResultTool(400,"获取失败");
        }
        User user = userService.queryByUsername(username);
        if (user == null){
            return new ResultTool(400,"获取失败");
        }
        List<User> cares = careService.getCares(username);
        return new ResultTool(cares,200);
    }


    @RequestMapping("/addCare")
    public ResultTool addCare(@RequestBody JSONObject jsonObject){
        String username = (String) jsonObject.get("username");
        String careUsername = (String) jsonObject.get("careUsername");
        User user = userService.queryByUsername(username);
        User careUser = userService.queryByUsername(careUsername);
        if (user == null || careUser == null){
            return new ResultTool(400,"关注失败");
        }

        Boolean result = careService.addCare(username, careUsername);
        if (result){
            return new ResultTool(200,"关注成功");
        }
        return new ResultTool(400,"关注失败");
    }

    @RequestMapping("/cancelCare")
    public ResultTool cancelCare(@RequestBody JSONObject jsonObject){
        String username = (String) jsonObject.get("username");
        String careUsername = (String) jsonObject.get("careUsername");
        User user = userService.queryByUsername(username);
        User careUser = userService.queryByUsername(careUsername);
        if (user == null || careUser == null){
            return new ResultTool(400,"取消关注失败");
        }
        Boolean tempResult = careService.queryCare(username, careUsername);
        if (tempResult){
            Boolean result = careService.cancelCare(username, careUsername);
            if (result){
                return new ResultTool(200,"取消关注成功");
            }
        }
        return new ResultTool(400,"取消关注失败");
    }

    @RequestMapping(value = "/queryCareByUserAndGoodId")
    public ResultTool queryCareByUserAndGoodId(@RequestBody JSONObject jsonObject){
        String username = (String) jsonObject.get("username");
        String careUsername = (String) jsonObject.get("careUsername");
        if (username == null || careUsername == null){
            return new ResultTool(400,"获取失败");
        }
        Boolean result;
        try {
//            result = collectService.queryCollect(username, goodId);
            result = careService.queryCare(username, careUsername);
        }catch (Exception e){
            e.printStackTrace();
            return new ResultTool(400,"查询失败");
        }
        if (result){
            return new ResultTool(true,200);
        }
        return new ResultTool(false,200);
    }

}
